package com.gtcmu.skeye;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.io.OutputStream;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;

import android.util.Log;
import android.widget.ImageView;
import android.widget.Toast;

public class PictureHandler implements PictureCallback {
	private final static String DEBUG_TAG = "PictureHandler";
	private final Context mContext;
	private final Camera c;
	Socket s;
	ImageView img;
	byte[] pictureData;
	  
	public PictureHandler(Context context,Socket s,Camera c, ImageView i) {
	    this.mContext = context;
	    this.s = s;
	    this.c = c;
	    this.img = i;
	  }
	 
	 public void sendPictureThread()
	    {
	    	new Thread(new Runnable() {
	            public void run() {
	            	try{
	       			 OutputStream socketOutputStream = s.getOutputStream();
	       			 socketOutputStream.write(pictureData);
	       			 s.close();
	       		 }
	       		 catch(IOException e)
	       		 {
	       			 Log.d(DEBUG_TAG, "Failed to Send image.");
	       		 }
	       		  
	       		 Log.d(DEBUG_TAG, "Sent image.");// do stuff that doesn't touch the UI here
	            }
	    }).start();
	    }
	 
	  @Override
	  public void onPictureTaken(byte[] data, Camera camera) {

		  Log.d(DEBUG_TAG, "onPictureTaken - called");   
		  if(c != null){
			  Bitmap bmp=BitmapFactory.decodeByteArray(data,0,data.length);
				Bitmap resizedBitmap = Bitmap.createScaledBitmap(bmp, 1024, 1024, false);
				img.setImageBitmap(bmp);
				img.requestFocus();
				Log.i("TcpClient", "Shown image ");
				Toast.makeText(mContext, "Shown Image", Toast.LENGTH_LONG).show();
		  c.startPreview();
		  pictureData = data.clone();
		  
		  }
	        	
	  }
	} 